package com.hh85.slidetab;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import java.util.List;

public class TabView extends LinearLayout {

    private Paint paint;

    private int x = 0;//开始的坐标
    private int y = 80;
    private int stopX = 100;
    private  int stopY = 80;

    private int textSelectedColor = Color.parseColor("#D81B60");
    private int textColor = Color.parseColor("#cccccc");
    private Integer textSize = 16;
   private  Integer textSelectedSize = 22;
    private IClickMenu iClickMenu;

    /**
     * 在java代码里new的时候会用到
     * @param context
     */
    public TabView(Context context) {
        super(context);
        initView(context,null);

    }



    /**
     * 在xml布局文件中使用时自动调用
     * @param context
     */
    public TabView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        initView(context,attrs);

    }

    /**
     * 不会自动调用，如果有默认style时，在第二个构造函数中调用
     * @param context
     * @param attrs
     * @param defStyleAttr
     */

    public TabView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * 只有在API版本>21时才会用到
     * 不会自动调用，如果有默认style时，在第二个构造函数中调用
     * @param context
     * @param attrs
     * @param defStyleAttr
     * @param defStyleRes
     */
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public TabView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        Log.i("TAG","开始绘画");
        View view =  getChildAt(0);

     // canvas.drawLine(x,view.getHeight()-10,stopX,view.getHeight()-10,paint);
        RectF oval3 = new RectF(x, view.getHeight() - 20, stopX, view.getHeight()-10);// 设置个新的长方形
        canvas.drawRoundRect(oval3,5,5,paint);
    }



    private void initView(Context context,AttributeSet attrs){

     //   Log.i("TAG","开始初始化");
        TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.tab);
       textSelectedColor = ta.getColor(R.styleable.tab_textSelectedColor,textSelectedColor);
       textColor = ta.getColor(R.styleable.tab_textColor,textColor);
       textSelectedSize = ta.getInt(R.styleable.tab_textSelectedSize,textSelectedSize);
       textSize = ta.getInt(R.styleable.tab_textSize,textSize);

        ta.recycle();

        paint = new Paint();
        paint.setColor(textSelectedColor);
        paint.setStyle(Paint.Style.FILL);

    }


    public void scroll(int position, float offset){

        Log.i("TAG","position:"+position+"offset:"+offset);

         // Log.i("TAG","子控件宽度"+ view.getWidth());
        int cout = getChildCount();
         int w =  getWidth() / cout; //子控件宽度
        Log.i("TAG","控件宽度"+ getWidth());
       x =  (int) (w *(offset + position))  + (w / 2 -30);
       stopX = x +  60;
       // scrollTo(x + (int)( w * offset),0); 超过范围滑动控件
     //   Log.i("TAG","x"+ x);
        invalidate();
    }

    public void setOnClickMenuListener(IClickMenu iClickMenu){

        this.iClickMenu = iClickMenu;
    };



    /**
     * 清楚文本颜色
     */
    private void clearTextViewColor(){
        for (int i=0;i<getChildCount();i++){
            View view = getChildAt(i);
            ((TextView)view).setTextColor(textColor);
            ((TextView)view).setTextSize(textSize);
        }
    }


    /**
     * 设置选中textview高亮
     * @param pos
     */
    public void highlightTextView(int pos){
        clearTextViewColor();
        View view = getChildAt(pos);
        ((TextView)view).setTextColor(textSelectedColor);
        ((TextView)view).setTextSize(textSelectedSize);
    }


    //当View中所有的子控件均被映射成xml后触发
    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
    }

    //当控件高宽发生变化
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

    }

    public void setTabMenuText(List<String> tabTexts){
        for (int i=0;i<tabTexts.size();i++){
            TextView textView = new TextView(getContext());
            textView.setText(tabTexts.get(i).toString());
            textView.setGravity(Gravity.CENTER);
            if(i == 0){
                textView.setTextColor(textSelectedColor);
                textView.setTextSize(textSelectedSize);
            }else{
                textView.setTextColor(textColor);
                textView.setTextSize(textSize);
            }


            textView.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT,1));
            final int index = i;
            textView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                  iClickMenu.clickMenu(index);
                }
            });
            addView(textView);
        }

    }

    public void setPos(int index){

        highlightTextView(index);
    }

}
